package cn.qf.shop.service.impl;

import cn.qf.shop.mapper.LetterMapper;
import cn.qf.shop.pojo.entity.Letter;
import cn.qf.shop.pojo.qo.LetterQO;
import cn.qf.shop.service.LetterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class LetterServiceImpl implements LetterService {
    private Logger logger = LoggerFactory.getLogger(LetterServiceImpl.class);
    @Resource
    LetterMapper letterMapper;
    public int addLet(LetterQO letterQO) {
        Letter letter = new Letter();
        BeanUtils.copyProperties(letterQO,letter);
        int i = letterMapper.insertLet(letter);
        return i;
    }

    public int updateLet(LetterQO letterQO) {
        Letter letter = new Letter();
        BeanUtils.copyProperties(letterQO,letter);
        int i = letterMapper.updateLet(letter);
        return i;
    }

    public int deleteLet(int letId) {
        int i = letterMapper.deleteLet(letId);
        return i;
    }

    public int deleteAllLet(List<Integer> letIds) {
        int i = letterMapper.deleteAllLet(letIds);
        return i;
    }

    public List<Letter> showLet() {
        List<Letter> letters = letterMapper.showAll();
        return letters;
    }

    public List<LetterQO> letterList(String keyword, String createTime, String updateTime, int limit, int offset) {
        List<Letter> letters = letterMapper.selectAll(keyword, createTime, updateTime, (limit-1)*offset, offset);
        if (letters == null) {
            return  null;
        }else {
            List<LetterQO> letterQOS = new ArrayList();
            for (Letter letter:letters) {
                LetterQO letterQO = new LetterQO();
                BeanUtils.copyProperties(letter,letterQO);
                letterQOS.add(letterQO);
            }
            return  letterQOS;
        }
    }
}
